{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 打印所有单行变量\n",
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = 'all'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 列表\n",
    "列表也是一种序列，每个元素以逗号`,`分开。列表无所不包，里面可以放各种类型的数据，且里面所能容纳的元素数量无限，  \n",
    "当然这是在硬件设备理想的情况下。`列表元素可以修改`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 基本操作\n",
    "和前面[《字符串》](./102.html)的基本操作类似，在list中，也有类似的操作。只不过是以**元素为单位**，不是以字符为单位进行索引了。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|       |              实例               |     结果      |                               描述                               |\n",
    "| :---: | :-----------------------------: | :-----------: | :--------------------------------------------------------------: |\n",
    "| len() |           len['ab', 1]           |       2       |                            求序列长度                            |\n",
    "|  `+`  |           ['ab']+[1]            |   ['ab', 1]   |                  连接2个序列，必须是同类型数据                   |\n",
    "|  `*`  |             ['a']*2             |  ['a', 'a']   |                           重复序列元素                           |\n",
    "|  in   | 'ab' in ['ab',1]<br>'a' in ['ab',1] | True<br>False |                     判断元素是否存在于序列中                     |\n",
    "| max() |         max(['a', 'b'])          |      'a'      | 返回最大值，大小排序符合字符编码，列表内元素必须是同种类型的数据 |\n",
    "| min() |           min([2, 1])            |       1       | 返回最小值，大小排序符合字符编码，列表内元素必须是同种类型的数据 |\n",
    "\n",
    ">用比较运算符比较大小，不过必须是同种类型的数据才能比较"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 索引和切片\n",
    "默认从0开始计数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a'"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'a'"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = ['a', 1, 'a', [3]]\n",
    "\n",
    "y[0]  # -->\n",
    "y[1]  # -->\n",
    "y.index('a')  # --> 检索该元素在列表中第一次出现的位置\n",
    "['a',1][0]  # -->"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|切片|结果|描述|\n",
    "|:---:|:---:|:---:|\n",
    "|list[start:end:step]|默认正向切片|切片取值：前包括后不包括<br>end必须大于start,否则返回空<br>step为空时，缺省值为1<br>step<0时，则反向切片|\n",
    "|y[1:]|[ 1, 'a', [3]]|得到从2号到最末尾的元素,这时最后那个可以不写|\n",
    "|y[:]|['a', 1, 'a', [3]]|得到所有元素|\n",
    "|y[:2]|['a', 1]|得到2之前(不包括第三个)的元素|\n",
    "|y[0:3:2]|['a', 'a']|得到以step=2从0到3(不包括3)的元素|\n",
    "|y[::-1]|[[3], 'a', 1, 'a']|反向得到所有元素|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果列表内有序列(字符串、列表等)，则可以进行二次索引和切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ab'"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = ['a', 1, 'abc', 2, [3]]\n",
    "\n",
    "y[2][:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 更新列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'python', 'abc']"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = ['a', 1, 'abc']\n",
    "y[1] = 'python'\n",
    "\n",
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 删除列表元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'abc', 'a']"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = ['a', 1, 'abc', 'a']\n",
    "del y[1]\n",
    "\n",
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 常用的列表函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['append',\n",
       " 'clear',\n",
       " 'copy',\n",
       " 'count',\n",
       " 'extend',\n",
       " 'index',\n",
       " 'insert',\n",
       " 'pop',\n",
       " 'remove',\n",
       " 'reverse',\n",
       " 'sort']"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[i for i in dir([]) if not i.startswith('_')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(...) method of builtins.list instance\n",
      "    L.append(object) -> None -- append object to end\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help([].append)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### list.append(obj)\n",
    "在列表末尾添加新的`对象`，修改原对象且`没有返回值`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "['python', 1, 2]\n"
     ]
    }
   ],
   "source": [
    "a = [\"python\", 1]\n",
    "print(a.append(2))  # 修改的是原始列表，没有返回值\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### list.extend(seq)\n",
    "在列表末尾一次性追加`另一个序列`中的多个值，修改原对象且`没有返回值`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['python', 1, 1, 2]"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [\"python\", 1]\n",
    "a.extend([1, 2])\n",
    "\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当追加的是字符串时，字符串会以字符为单位拆开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['python', 1, 1, 2, 'a', 'b']"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.extend('ab')\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### list.insert(index,obj)\n",
    "将对象`obj`插入到列表中`index`位置，原位置向后移动。修改原对象且`没有返回值`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['python', 'a', 1]"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [\"python\", 1]\n",
    "a.insert(1, 'a')\n",
    "\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### list.copy()\n",
    "`返回`复制后的新列表，类似a[:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### list.clear()\n",
    "清空列表，类似del a[:]，修改原对象且`没有返回值`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### list.count(obj)\n",
    "`返回`某个元素在列表中出现的次数，不存在则返回0，不会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1, 2, 1, 1, 3]\n",
    "a.count(1)  # -->\n",
    "a.count(5)  # -->"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### list.index(obj)\n",
    "检索该元素在列表中第一次出现的位置。`返回`查找对象的索引位置，如果没有找到对象则抛出异常"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### list.pop([index])\n",
    "移除列表中的一个元素（默认最后一个元素），修改原对象并且`返回`该元素的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "['python', 1, 1]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'python'"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[1, 1]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [\"python\", 1, 1, 2]\n",
    "\n",
    "a.pop()   # -->\n",
    "a         # -->\n",
    "a.pop(0)  # -->\n",
    "a         # -->"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### list.remove(obj)\n",
    "移除列表中某个值的`第一个`匹配项。修改原对象且`没有返回值`，如果所删除的内容不在列表中，就报错。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### list.reverse()\n",
    "将列表的元素顺序反过来。修改原对象且`没有返回值`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[6, 1, 5, 3]"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [3,5,1,6]\n",
    "a.reverse()\n",
    "\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### list.sort(key=None, reverse=False)\n",
    "key接受一个函数，这个函数只接受一个元素，默认为None  \n",
    "reverse是一个布尔值。如果设置为True，列表元素将被倒序排列，默认为False  \n",
    "修改原对象且`没有返回值`。默认情况，实现的是从小到大的排序。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[6, 5, 3, 1]"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [3,5,1,6]\n",
    "a.sort(reverse=True)\n",
    "\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "python3内置函数sorted(iterable，key=None,reverse=False)可`返回`一个列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "students = [('john', 'A', 15), ('jane', 'B', 12), ('dave','B', 10)]\n",
    "sorted(students,key=lambda s: students[2]) # 按照年龄来排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 元组\n",
    "元组使用小括号`(` `)`每个元素以逗号`,`分开。元组与列表类似，不同之处在于元组的元素不能修改\n",
    "\n",
    "注意：元组中只包含一个元素时，需要在元素后面添加逗号\n",
    "\n",
    "### 索引和切片\n",
    "元组的基本操作就和列表类似。\n",
    "\n",
    "###  元组用在哪里？\n",
    "元组有这类特点，并且是它使用的情景:\n",
    "- 元组比列表操作速度快。如果您定义了一个值的常量集，并且唯一要用它做的是不断地遍历它，请使用元组代替列表。\n",
    "- 如果对不需要修改的数据进行 “写保护”，可以使代码更安全\n",
    "- 元组可以在字典中被用做 key，但是列表不行\n",
    "- 元组可以用在字符串格式化中。\n"
   ]
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4rc1"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {
    "height": "839px",
    "left": "0px",
    "right": "1612px",
    "top": "107px",
    "width": "308px"
   },
   "toc_section_display": "block",
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": "15",
    "lenType": "10",
    "lenVar": "60"
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
